import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { Router } from '@angular/router';

import { ConfirmModalService } from '@core/base';
import { Config, CustomService, Task } from '@core/job/custom';
import { NzMessageService } from 'ng-zorro-antd/message';

import { CustomConfigFormService } from './custom-config-form.service';

@Component({
  selector: 'app-custom-task-desc',
  templateUrl: './custom-task-desc.component.html',
  providers: [CustomConfigFormService]
})
export class CustomTaskDescComponent implements OnInit {
  @Input() configId?: string;

  task?: Task;
  isLoading = false;

  constructor(
    private customSrc: CustomService,
    private confirmModalSrc: ConfirmModalService,
    private messageSrc: NzMessageService,
    private customConfigFormSrc: CustomConfigFormService,
    private router: Router
  ) {}

  ngOnInit(): void {
    this.loadTask();
  }

  loadTask(): void {
    if (this.configId) {
      this.isLoading = true;
      this.customSrc.getTask(this.configId).subscribe(data => {
        this.task = data;
        this.isLoading = false;
      });
    }
  }

  stopTask(): void {
    if (!this.task?.scheduled) {
      this.messageSrc.error('任务已经停止，不能重复停止');
      return;
    }

    this.confirmModalSrc.danger('确认停止任务吗？任务停止后可以再次启动').subscribe(ok => {
      if (ok && this.task) {
        this.customSrc.stopTask(this.task?.configId).subscribe(data => {
          this.loadTask();
          this.messageSrc.success('停止任务成功');
        });
      }
    });
  }

  startTask(): void {
    if (!this.task?.enabled) {
      this.messageSrc.error('任务配置是禁用状态，不能启动');
      return;
    }
    this.confirmModalSrc.danger('确认启动任务吗？').subscribe(ok => {
      if (ok && this.task) {
        this.customSrc.startTask(this.task?.configId).subscribe(data => {
          this.loadTask();
          this.messageSrc.success('启动任务成功');
        });
      }
    });
  }

  editConfig(): void {
    if (this.task?.scheduled) {
      this.messageSrc.error('任务已经启动，不能编辑任务配置');
      return;
    }

    if (this.task?.configId) {
      this.customSrc.getConfigById(this.task.configId).subscribe(data => this.openForm(data));
    }
  }

  deleteConfig(): void {
    if (this.task?.scheduled) {
      this.messageSrc.error('任务已经启动，不能删除任务配置');
      return;
    }

    this.confirmModalSrc.danger('确认删除任务配置吗？').subscribe(ok => {
      if (ok && this.task) {
        this.customSrc.deleteConfig(this.task?.configId).subscribe(() => {
          this.messageSrc.success('删除任务配置成功');
          this.router.navigateByUrl('/job');
        });
      }
    });
  }

  openForm(config: Config): void {
    const modal = this.customConfigFormSrc.open('编辑任务配置', { config: config });

    modal.afterClose.subscribe(formValue => {
      if (formValue) {
        this.customSrc.updateConfig(config.id, formValue).subscribe(() => {
          this.loadTask();
          this.messageSrc.success('编辑任务配置成功');
        });
      }
    });
  }

  deleteLogs(): void {
    this.confirmModalSrc.danger('确认删除此任务的所有日志吗？').subscribe(ok => {
      if (ok && this.task) {
        this.customSrc.deleteLogs(this.task?.configId).subscribe(() => {
          this.loadTask();
          this.messageSrc.success('删除任务日志成功');
        });
      }
    });
  }
}
